#!/usr/bin/perl -w

#=======================================================================
# getsvnfiles
# File ID: 21045066-f743-11dd-9679-000475e441b9
# [Description]
#
# Character set: UTF-8
# ©opyleft 2008– Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later, see end of 
# file for legal stuff.
#=======================================================================

use strict;
use Getopt::Long;
use File::Temp;

$| = 1;

our $Debug = 0;

our %Opt = (

    'debug' => 0,
    'dest' => "",
    'dry-run' => 0,
    'help' => 0,
    'revisions' => "",
    'source' => "",
    'verbose' => 0,
    'version' => 0,

);

our $progname = $0;
$progname =~ s/^.*\/(.*?)$/$1/;
our $VERSION = "0.00";

Getopt::Long::Configure("bundling");
GetOptions(

    "debug" => \$Opt{'debug'},
    "dest|d=s" => \$Opt{'dest'},
    "dry-run" => \$Opt{'dry-run'},
    "help|h" => \$Opt{'help'},
    "revisions|r=s" => \$Opt{'revisions'},
    "source|s=s" => \$Opt{'source'},
    "verbose|v+" => \$Opt{'verbose'},
    "version" => \$Opt{'version'},

) || die("$progname: Option error. Use -h for help.\n");

$Opt{'debug'} && ($Debug = 1);
$Opt{'help'} && usage(0);
if ($Opt{'version'}) {
    print_version();
    exit(0);
}

my $Lh = "[0-9a-fA-F]";
my $v1_templ = "$Lh\{8}-$Lh\{4}-1$Lh\{3}-$Lh\{4}-$Lh\{12}";

my $CMD_SVN = "svn";
my $PROP_PREFIX = "getsvnfiles:";
my %Prop = (
    'author' => "${PROP_PREFIX}author",
    'date' => "${PROP_PREFIX}date",
    'log' => "${PROP_PREFIX}log",
    'revision' => "${PROP_PREFIX}revision",
    'source' => "${PROP_PREFIX}source",
);

my @Revs = ();
my ($start_rev, $end_rev);

my $missing_args = 0;
for my $chkdef (qw{source dest revisions}) {
    length($Opt{$chkdef}) ||
        (warn("$progname: --$chkdef option not defined\n"), $missing_args = 1);
}
$missing_args && exit(1);

if ($Opt{'revisions'} =~ /^(\d+):(\d+|head)$/i) {
    ($start_rev, $end_rev) = (uc($1), uc($2));
} else {
    die("$progname: \"$Opt{'revisions'}\": " .
        "Invalid format of --revisions argument\n");
}

if (!-f $Opt{'dest'}) {
    die("$progname: $Opt{'dest'}: File does not exist\n");
}

@Revs = revisions($Opt{'source'}, $start_rev, $end_rev);

D(sprintf("\@Revs = '%s'\n", join(", ", @Revs)));

my $tmpfile_logmsg = mktemp("$progname-logmsg-XXXXXX");
my $tmpfile_propset = mktemp("$progname-propset-XXXXXX");
mysyst($CMD_SVN, "update", $Opt{'dest'});
for my $curr_rev (@Revs) {
    print(STDERR "======== $progname: Downloading revision $curr_rev ========\n");

    if (!$Opt{'dry-run'}) {
        my %rev_info = revision_info($Opt{'source'}, $curr_rev);
        my $log_msg = log_message($Opt{'source'}, $curr_rev);
        if (open(LogMsgFP, ">", $tmpfile_logmsg)) {
            print(LogMsgFP prepare_logmsg($log_msg));
            chomp(my $uuid = `suuid -t $progname -w eo -c "Commit r$curr_rev of $Opt{'dest'} from $Opt{'source'}"`);
            print(LogMsgFP "\n$uuid");
            close(LogMsgFP);
            if (!defined($uuid) || $uuid !~ /^$v1_templ$/) {
                unlink($tmpfile_logmsg)
                    || warn("$progname: $tmpfile_logmsg: Cannot delete temp file: $!\n");
                die("$progname: suuid error, aborting.\n");
            }
        } else {
            die("$progname: $tmpfile_logmsg: Cannot create file: $!\n");
        }
        if (open(CatFP, "$CMD_SVN cat $Opt{'source'} -r$curr_rev |")) {
            if (open(DestFP, ">", $Opt{'dest'})) {
                while (<CatFP>) {
                    print(DestFP $_);
                }
                close(DestFP);
            } else {
                die("$progname: $Opt{'dest'}: Cannot open file for write: $!\n");
            }
            close(CatFP);
        } else {
            die("$progname: Cannot open \"svn cat\" pipe: $!\n");
        }
        svn_propset($Prop{'source'}, $Opt{'source'}, $Opt{'dest'});
        svn_propset($Prop{'revision'}, $curr_rev, $Opt{'dest'});
        svn_propset($Prop{'date'}, $rev_info{'date'}, $Opt{'dest'});
        svn_propset($Prop{'author'}, $rev_info{'author'}, $Opt{'dest'});
        svn_propset($Prop{'log'}, $rev_info{'log'}, $Opt{'dest'});
        mysyst($CMD_SVN, "commit", "-F", $tmpfile_logmsg, $Opt{'dest'});
    }
}
if (!$Opt{'dry-run'}) {
    mysyst($CMD_SVN, "propdel", $Prop{'author'}, $Opt{'dest'});
    mysyst($CMD_SVN, "propdel", $Prop{'date'}, $Opt{'dest'});
    mysyst($CMD_SVN, "propdel", $Prop{'log'}, $Opt{'dest'});
    mysyst($CMD_SVN, "propdel", $Prop{'revision'}, $Opt{'dest'});
    mysyst($CMD_SVN, "propdel", $Prop{'source'}, $Opt{'dest'});
    my $file_loc = file_location($Opt{'dest'});
    if (open(LogMsgFP, ">", $tmpfile_logmsg)) {
        print(LogMsgFP
            sprintf(
                "%s: %u revision%s between r%s:%s downloaded from\n" .
                "<%s>.\n" .
                "\n" .
                "* %s\n" .
                "  Deleted download properties.\n" .
                "\n" .
                "%s",
                $progname, scalar(@Revs), scalar(@Revs) == 1 ? "" : "s",
                $start_rev, $end_rev, $Opt{'source'},
                file_location($Opt{'dest'}),
                `suuid -t $progname -w eo -c "Delete download properties from $Opt{'dest'}, downloaded from $Opt{'source'}"`
            )
        );
        close(LogMsgFP);
    } else {
        die("$progname: $tmpfile_logmsg: Cannot open file for write: $!\n");
    }

    mysyst($CMD_SVN, "commit", "-F", $tmpfile_logmsg, $Opt{'dest'});
    unlink($tmpfile_logmsg);
}

sub file_location {
    # Find the repository location for a file {{{
    my $File = shift;
    my $Info = `$CMD_SVN info --xml $File`;
    my ($Url, $Root) = ("", "");
    $Info =~ /<url>(.*?)<\/url>/s && ($Url = $1);
    $Info =~ /<root>(.*?)<\/root>/s && ($Root = $1);
    my $Retval = substr($Url, length($Root));
    return($Retval);
    # }}}
} # file_location()

sub svn_propset {
    # Set file property for a file {{{
    my ($Propname, $Propval, $Dest) = @_;
    D("svn_propset('$Propname', '$Propval', '$Dest');");
    if (open(PropFP, ">", $tmpfile_propset)) {
        print(PropFP $Propval);
        close(PropFP);
    } else {
        die("$progname: $tmpfile_propset: Cannot create file: $!\n");
    }
    mysyst($CMD_SVN, "propset", $Propname, "-F", $tmpfile_propset, $Dest);
    unlink($tmpfile_propset);
    # }}}
} # svn_propset()

sub prepare_logmsg {
    # {{{
    my $Txt = shift;
    $Txt = "# $Txt";
    $Txt =~ s/(\n)/$1# /gs;
    $Txt =~ s/# $//s;
    $Txt =~ s/\n# \n/\n#\n/gs;
    $Txt =~ s/\n# \n/\n#\n/gs;
    $Txt =~ s/#\n(# ------------------------------------------------------------------------\n)/$1/gs;
    $Txt =~ s/^# ------------------------------------------------------------------------\n//s;
    $Txt =~ s/# ------------------------------------------------------------------------\n$//s;
    return($Txt);
    # }}}
} # prepare_logmsg()

sub log_message {
    # Return log message for a specific revision {{{
    my ($Source, $Rev) = @_;
    my $Retval = `$CMD_SVN log -r$Rev $Source`;
    return($Retval);
    # }}}
} # log_message()

sub revision_info {
    # Return raw log message for a specific revision {{{
    my ($Source, $Rev) = @_;
    my %Retval = ();
    my $Xml = `$CMD_SVN log --xml -r$Rev $Source`;
    $Xml =~ /<author>(.*?)<\/author>/s && ($Retval{'author'} = xml_to_txt($1));
    $Xml =~ /<date>(.*?)<\/date>/s && ($Retval{'date'} = xml_to_txt($1));
    $Xml =~ /<msg>(.*?)<\/msg>/s && ($Retval{'log'} = xml_to_txt($1));
    # $Retval =~ s/^.*<msg>(.*?)<\/msg>.*$/$1/s;
    return(%Retval);
    # }}}
} # revision_info()

sub revisions {
    # Return an array of revision numbers from a specific revision range 
    # for a version controlled element
    # {{{
    my ($File, $Start, $End) = @_;
    D("revisions('$File', '$Start', '$End')");
    my $safe_file = escape_filename($File);
    my $Data = "";
    my @Revs = ();

    if (open(PipeFP, "$CMD_SVN log --xml -r$Start:$End $safe_file |")) {
        $Data = join("", <PipeFP>);
        close(PipeFP);
        $Data =~ s/<logentry\b.*?\brevision="(\d+)".*?>/push(@Revs, "$1")/egs;
    }
    return(@Revs);
    # }}}
} # revisions()

sub escape_filename {
    # Kludge for handling file names with spaces and characters that 
    # trigger shell functions
    # {{{
    my $Name = shift;
    # $Name =~ s/\\/\\\\/g;
    # $Name =~ s/([ \t;\|!&"'`#\$\(\)<>\*\?])/\\$1/g;
    $Name =~ s/'/\\'/g;
    $Name = "'$Name'";
    return($Name);
    # }}}
} # escape_filename()

sub xml_to_txt {
    # Convert XML data to plain text {{{
    my $Txt = shift;
    $Txt =~ s/&lt;/</gs;
    $Txt =~ s/&gt;/>/gs;
    $Txt =~ s/&amp;/&/gs;
    return($Txt);
    # }}}
} # xml_to_txt()

sub mysyst {
    # Customised system() {{{
    my @Args = @_;
    msg(1, sprintf("%s \"%s\"", $Opt{'dry-run'} ? "Simulating" : "Executing", join(" ", @Args)));
    $Opt{'dry-run'} || system(@Args);
    # }}}
} # mysyst()

sub deb_wait {
    # Wait until Enter is pressed if --debug {{{
    $Debug || return;
    print(STDERR "debug: Press ENTER...");
    <STDIN>;
    # }}}
} # deb_wait()

sub print_version {
    # Print program version {{{
    print("$progname v$VERSION\n");
    # }}}
} # print_version()

sub usage {
    # Send the help message to stdout {{{
    my $Retval = shift;

    if ($Opt{'verbose'}) {
        print("\n");
        print_version();
    }
    print(<<END);

Usage: $progname [OPTIONS] -s SOURCE -d DEST -r STARTREV:ENDREV

Copy all revisions of a file from a remote repository into a local file 
and commit it with the log message from the remote repository. Does not 
handle file properties yet.

Options:

  -d X, --dest X
    Use X as local file destination.
  --dry-run
    Don’t make any changes, only simulate.
  -h, --help
    Show this help.
  -r X:Y, --revision X:Y
    Copy revision range X:Y from remote repository.
  -s X, --source X
    Source file to download from remote repository.
  -v, --verbose
    Increase level of verbosity. Can be repeated.
  --version
    Print version information.
  --debug
    Print debugging messages.

END
    exit($Retval);
    # }}}
} # usage()

sub msg {
    # Print a status message to stderr based on verbosity level {{{
    my ($verbose_level, $Txt) = @_;

    if ($Opt{'verbose'} >= $verbose_level) {
        print(STDERR "$progname: $Txt\n");
    }
    # }}}
} # msg()

sub D {
    # Print a debugging message {{{
    $Debug || return;
    my @call_info = caller;
    chomp(my $Txt = shift);
    my $File = $call_info[1];
    $File =~ s#\\#/#g;
    $File =~ s#^.*/(.*?)$#$1#;
    print(STDERR "$File:$call_info[2] $$ $Txt\n");
    return("");
    # }}}
} # D()

__END__

# Plain Old Documentation (POD) {{{

=pod

=head1 NAME



=head1 SYNOPSIS

 [options] [file [files [...]]]

=head1 DESCRIPTION



=head1 OPTIONS

=over 4

=item B<-h>, B<--help>

Print a brief help summary.

=item B<-v>, B<--verbose>

Increase level of verbosity. Can be repeated.

=item B<--version>

Print version information.

=item B<--debug>

Print debugging messages.

=back

=head1 BUGS



=head1 AUTHOR

Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.

=head1 COPYRIGHT

Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
This is free software; see the file F<COPYING> for legalese stuff.

=head1 LICENCE

This program is free software: you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation, either version 2 of the License, or (at your 
option) any later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program.
If not, see L<http://www.gnu.org/licenses/>.

=head1 SEE ALSO

=cut

# }}}

# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
